home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / ezview / ulstrcmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-06  |  3.3 KB  |  122 lines

  1. /*
  2. Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
  3.  
  4. Permission to use, copy, modify, and distribute this material 
  5. for any purpose and without fee is hereby granted, provided 
  6. that the above copyright notice and this permission notice 
  7. appear in all copies, and that the name of Bellcore not be 
  8. used in advertising or publicity pertaining to this 
  9. material without the specific, prior written permission 
  10. of an authorized representative of Bellcore.  BELLCORE 
  11. MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
  12. OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
  13. WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
  14. */
  15. /* ********************************************************************** *\
  16.  *         Copyright IBM Corporation 1988,1989 - All Rights Reserved      *
  17.  *        For full copyright information see:'andrew/config/COPYRITE'     *
  18. \* ********************************************************************** */
  19. /*
  20.     ulstrcmp.c--compare strings ignoring alphabetic case.
  21. */
  22.  
  23. /* $Header: /afs/.andrew.cmu.edu/itc/sm/releases/X.V11R4/andrew/overhead/util/lib/RCS/ulstrcmp.c,v 2.6 89/08/24 12:08:25 cfe Exp $ */
  24. /* $ACIS: $ */
  25. /* $Source: /afs/.andrew.cmu.edu/itc/sm/releases/X.V11R4/andrew/overhead/util/lib/RCS/ulstrcmp.c,v $ */
  26.  
  27. #ifndef lint
  28. static char *rcsid = "$Header: /afs/.andrew.cmu.edu/itc/sm/releases/X.V11R4/andrew/overhead/util/lib/RCS/ulstrcmp.c,v 2.6 89/08/24 12:08:25 cfe Exp $";
  29. #endif /* lint */
  30.  
  31. #include <ctype.h>
  32.  
  33.  
  34. int ULstrcmp(s1, s2)
  35. register char *s1, *s2;
  36. {
  37.     /* case INSENSITIVE:  Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
  38.       */
  39.     register char c1,c2;
  40.  
  41.     for(;;) {
  42.     c1 = *s1++; if (c1 <= 'Z') if (c1 >= 'A') c1 += 040;
  43.     c2 = *s2++; if (c2 <= 'Z') if (c2 >= 'A') c2 += 040;
  44.     if (c1 != c2) break;
  45.     if (c1 == '\0') return(0);
  46.     }
  47.     return(c1 - c2);
  48. }
  49.  
  50. #ifdef AMIGA
  51. #ifndef isascii
  52. #define isascii(c)      ((unsigned)(c)<=127)
  53. #endif
  54. #endif
  55.  
  56. #define DOWNCASE(x) (isascii(x) && isalpha(x) && isupper(x) ? (tolower(x)) : (x) )
  57.  
  58. int ULstrncmp(s1,s2,n)
  59. char *s1, *s2;
  60. int n;
  61. {
  62.   /* case INSENSITIVE:  Compare strings, up to n chars:  
  63.      s1>s2: >0  s1==s2: 0  s1<s2: <0
  64.    */
  65.  
  66.   register int i;
  67.   register int result = 0;
  68.  
  69.   for(i = 0;(s1[i] || s2[i]) && i<n && !result;++i){
  70.     result = DOWNCASE(s2[i]) - DOWNCASE(s1[i]);
  71.   }
  72.   return(result);
  73. }
  74.  
  75. /* Just like strncpy but shift-case in transit and forces null termination */
  76. /* Copied 8/24/89 from afs/util/casestrcpy.c to allow omission of lib/afs/util.a */
  77.  
  78. char *lcstring (d, s, n)
  79.   char *d;                /* dest string */
  80.   char *s;                /* source string */
  81.   int   n;                /* max transfer size */
  82. {   char *original_d = d;
  83.     char  c;
  84.  
  85.     if ((s == 0) || (d == 0)) return 0;    /* just to be safe */
  86.     while (n) {
  87.     c = *s++;
  88.     c = DOWNCASE(c);
  89.     *d++ = c;
  90.     if (c == 0) break;        /* quit after transferring null */
  91.     if (--n == 0) *(d-1) = 0;    /* make sure null terminated */
  92.     }
  93.     return original_d;
  94. }
  95.  
  96.  
  97. #undef DOWNCASE
  98.  
  99. #ifdef TESTINGONLYTESTING
  100. #include <stdio.h>
  101. main(argc,argv)
  102. int argc;
  103. char *argv[];
  104. {
  105.   int result = 0;
  106.  
  107.   switch(argc) {
  108.   case 3:
  109.     result = ULstrcmp(argv[1], argv[2]);
  110.     break;
  111.   case 4:
  112.     result = ULstrncmp(argv[1], argv[2], atoi(argv[3]));
  113.     break;
  114.   default:
  115.     fprintf(stderr, "usage: ulstrcmp s1 s2 [n].\n");
  116.     exit(1);
  117.   }
  118.   printf("'%s' %s '%s'.\n",
  119.      argv[1], (result == 0)?"==":(result<0)?"<":">", argv[2]);
  120. }
  121. #endif /* TESTINGONLYTESTING */
  122.